package com.cooba.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;

public class FileOpeartionUtil {
    private static String sdState = Environment.getExternalStorageState();
    private static String path = Environment.getExternalStorageDirectory().toString();
    private static String dir = "/Cooba";
    private static String filename = "file.tmp";
    public static final String TEMP_PHOTO_FILE_NAME = "/Cooba/temp_photo.jpg";

	public static void saveFile(String content) {
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {
			file = new File(path + dir);
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(path + dir, filename);
			try {
				// if (!file.exists()) {
				// file.createNewFile();
				// }
				FileOutputStream fos = new FileOutputStream(file);
				fos.write(content.getBytes());
				fos.flush();
				fos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String getFileContent() {
		String str = null;
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {

			file = new File(path + dir, filename);
			if (file.exists()) {
				byte[] buffer = new byte[256];
				int length = 0;
				try {
					length = new FileInputStream(file).read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}

				return new String(buffer, 0, length);
			}
		}
		return str;
	}

	public static void deleteFile(String name) {
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {
			file = new File(path + dir, name);
			if (file.exists()) {
				if (file.isFile()) {
					file.delete();
				}
				// 如果它是一个目录
				else if (file.isDirectory()) {
					File files[] = file.listFiles();
					for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
						deleteFile(files[i].getName()); // 把每个文件 用这个方法进行迭代
					}
				}
				file.delete();
			}
		}
	}

	public static boolean fileExsit(String name) {
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {

			file = new File(path + dir, name);
			if (file.exists()) {
				return true;
			}
		}
		return false;
	}

	public static long getFileSize(String name) {
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {

			file = new File(path + dir, name);
			if (file.exists()) {
				return file.length();
			}
		}
		return 0;
	}

	public static File getFile(String name) {
		File file;
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {

			file = new File(path + dir, name);
			if (file.exists()) {
				return file;
			}
		}
		return null;
	}

	public static String getFilePath(String name) {
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {

			File file = new File(path + dir, name);
			return file.getPath();
		}
		return null;
	}

	public static void write2File(String filename, String content) {
		if (sdState.equals(Environment.MEDIA_MOUNTED)) {
			try {
				File file;// = getFile(filename);
				file = new File(path + dir);
				if (!file.exists()) {
					file.mkdirs();
				}
				file = new File(path + dir, filename);

				long time = System.currentTimeMillis();
				String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
						.format(new Date(time));
				FileWriter fw = new FileWriter(file, true);
				fw.append(date + " : ");
				fw.append(content);
				fw.append('\n');
				fw.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
